from __future__ import absolute_import
from chips.jsonlib.types.ext import IsValueExt as _IsValueExt
from chips.jsonlib.types.ext import schemas as _sch

# Hack to import python's std library datetime module when calling this file
# as a script, rather than a module
try:
    import sys
    sys.path = sys.path[1:] + [sys.path[0]]
    from datetime import *
finally:
    sys.path = [sys.path[-1]] + sys.path[:-1]
    del sys

class JSONDate(_IsValueExt):
    '''
    # JSONExt types are abstract types
    >>> isinstance(date(2000,1,1), JSONDate)
    True
    
    # They also have methods to convert between the Python type and JSON.
    >>> print JSONDate.to_json(date(2000,1,1))
    {u'year': 2000, u'day': 1, u'month': 1}
    
    >>> print JSONDate.from_json({'month': 1, 'day': 1, 'year': 2000})
    2000-01-01
    '''
    type = date
    schema = {u'year': _sch.Int(), u'month': _sch.Int(), u'day': _sch.Int()}

class JSONTime(_IsValueExt):
    '''
    >>> isinstance(time(12,0,0), JSONTime)
    True
    
    # They also have methods to convert between the Python type and JSON.
    >>> print JSONTime.to_json(time(12,0,0))
    {u'hour': 12}
    >>> print JSONTime.from_json({'hour': 12})
    12:00:00
    '''
    type = time
    schema = {u'hour?': _sch.Int(0), u'minute?': _sch.Int(0), u'second?': _sch.Int(0),
              u'microsecond?': _sch.Int(0), u'tzinfo?': _sch.Int() }

class JSONDateTime(_IsValueExt):
    type = datetime
    schema = {u'year': _sch.Int(), u'month': _sch.Int(), u'day': _sch.Int(),
              u'hour?': _sch.Int(0), u'minute?': _sch.Int(0), u'second?': _sch.Int(0),
              u'microsecond?': _sch.Int(0), u'tzinfo?': _sch.Int()}

if __name__ == '__main__':
    import doctest
    doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE, verbose=0)
